﻿using System;
using System.Collections.Generic;
using System.Threading;

namespace ParasiTracer
{
    public class Tracer
    {
        public static void inFunc(int fid)
        {
            ProgramContext.inFunc(fid);

            if (FuncTable.inst.funcs[fid].enabled)
            {
                string msg = string.Format(
                    "{0} entra: {1} \r\n",
                    Thread.CurrentThread.ManagedThreadId,
                    FuncTable.inst.funcs[fid].name
                );

                TracerIO.inst.write(msg);
            }
        }

        public static int inFuncWithParams(int fid)
        {
            ProgramContext.inFunc(fid);

            if (FuncTable.inst.funcs[fid].enabled)
            {
                string msg = string.Format(
                    "{0} entra: {1} \r\n",
                    Thread.CurrentThread.ManagedThreadId,
                    FuncTable.inst.funcs[fid].name
                );

                TracerIO.inst.write(msg);
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public static void outFunc(int fid)
        {
            if (FuncTable.inst.funcs[fid].enabled)
            {
                string msg = string.Format(
                    "{0} sale : {1} \r\n",
                    Thread.CurrentThread.ManagedThreadId,
                    FuncTable.inst.funcs[fid].name
                );

                TracerIO.inst.write(msg);
            }

            ProgramContext.outFunc(fid);
        }

        public static void throwFunc(int fid)
        {
            if (FuncTable.inst.funcs[fid].enabled)
            {
                string msg = string.Format(
                    "{0} throw: {1} \r\n",
                    Thread.CurrentThread.ManagedThreadId,
                    FuncTable.inst.funcs[fid].name
                );

                TracerIO.inst.write(msg);
            }
        }

        public static void doneParams (int fid)
        {
            // no es realmente necesario ya que sabemos de antemano
            // la cantidad de parametros, pero es mas comodo y no
            // tenemos que leer la cantidad ni velar por ella.
            
            // pares: (nParam:int, typeParam:int)

            // ¿tengo funcion en lua? (se lo pregunto a la functable por perf)
            // si: 
            //      llamo a la funcion... (ella tiene el poder de hacer cualquier cosa)

        }

        public static void booleanParam (bool param)    { TracerIO.inst.write(string.Format("\t{0} \r\n", param)); }
        public static void charParam    (char param)    { TracerIO.inst.write(string.Format("\t{0} \r\n", param)); }
        public static void sbyteParam   (sbyte param)   { TracerIO.inst.write(string.Format("\t{0} \r\n", param)); }
        public static void byteParam    (byte param)    { TracerIO.inst.write(string.Format("\t{0} \r\n", param)); }
        public static void int16Param   (short param)   { TracerIO.inst.write(string.Format("\t{0} \r\n", param)); }
        public static void uint16Param  (ushort param)  { TracerIO.inst.write(string.Format("\t{0} \r\n", param)); }
        public static void int32Param   (int param)     { TracerIO.inst.write(string.Format("\t{0} \r\n", param)); }
        public static void uint32Param  (uint param)    { TracerIO.inst.write(string.Format("\t{0} \r\n", param)); }
        public static void int64Param   (long param)    { TracerIO.inst.write(string.Format("\t{0} \r\n", param)); }
        public static void uint64Param  (ulong param)   { TracerIO.inst.write(string.Format("\t{0} \r\n", param)); }
        public static void singleParam  (float param)   { TracerIO.inst.write(string.Format("\t{0} \r\n", param)); }
        public static void doubleParam  (double param)  { TracerIO.inst.write(string.Format("\t{0} \r\n", param)); }
        public static void stringParam  (string param)  { TracerIO.inst.write(string.Format("\t{0} \r\n", param)); }
        public static void intptrParam  (IntPtr param)  { TracerIO.inst.write(string.Format("\t{0} \r\n", param)); }
        public static void uintptrParam (UIntPtr param) { TracerIO.inst.write(string.Format("\t{0} \r\n", param)); }
        public static void objectParam  (object param)  { TracerIO.inst.write(string.Format("\t{0} \r\n", param)); }

        

        public static void booleanRetVal(bool retVal)   { TracerIO.inst.write(string.Format("\t{0} \r\n", retVal)); }
        public static void charRetVal   (char retVal)   { TracerIO.inst.write(string.Format("\t{0} \r\n", retVal)); }
        public static void sbyteRetVal  (sbyte retVal)  { TracerIO.inst.write(string.Format("\t{0} \r\n", retVal)); }
        public static void byteRetVal   (byte retVal)   { TracerIO.inst.write(string.Format("\t{0} \r\n", retVal)); }
        public static void int16RetVal  (short retVal)  { TracerIO.inst.write(string.Format("\t{0} \r\n", retVal)); }
        public static void uint16RetVal (ushort retVal) { TracerIO.inst.write(string.Format("\t{0} \r\n", retVal)); }
        public static void int32RetVal  (int retVal)    { TracerIO.inst.write(string.Format("\t{0} \r\n", retVal)); }
        public static void uint32RetVal (uint retVal)   { TracerIO.inst.write(string.Format("\t{0} \r\n", retVal)); }
        public static void int64RetVal  (long retVal)   { TracerIO.inst.write(string.Format("\t{0} \r\n", retVal)); }
        public static void uint64RetVal (ulong retVal)  { TracerIO.inst.write(string.Format("\t{0} \r\n", retVal)); }
        public static void singleRetVal (float retVal)  { TracerIO.inst.write(string.Format("\t{0} \r\n", retVal)); }
        public static void doubleRetVal (double retVal) { TracerIO.inst.write(string.Format("\t{0} \r\n", retVal)); }
        public static void stringRetVal (string retVal) { TracerIO.inst.write(string.Format("\t{0} \r\n", retVal)); }
        public static void intptrRetVal (IntPtr retVal) { TracerIO.inst.write(string.Format("\t{0} \r\n", retVal)); }
        public static void uintptrRetVal(UIntPtr retVal){ TracerIO.inst.write(string.Format("\t{0} \r\n", retVal)); }
        public static void objectRetVal (object retVal) { TracerIO.inst.write(string.Format("\t{0} \r\n", retVal)); }
    }
}
